﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace DAL
{
    public class FeedBack
    {
        DbHelperSQL dbhelper = new DbHelperSQL();

        #region 根据FId获取反馈信息
        /// <summary>
        /// 根据FId获取反馈信息
        /// </summary>
        /// <param name="lid"></param>
        /// <returns></returns>
        public MODEL.FeedBack getModelByFid(int fid)
        {
            string sql = "select * from FeedBack fb join Users u on fb.UserId=u.UId where FId=@FId;";
            SqlParameter[] paras = { 
                                      new SqlParameter("@FId",fid)
                                   };
            DataRow dr = dbhelper.GetDataRow(sql, paras);
            if (dr != null)
            {
                MODEL.FeedBack model = new MODEL.FeedBack();
                setDr2Model(dr, model);
                return model;
            }
            else
            {
                return null;
            }
        } 
        #endregion

        #region 根据FId号执行软删除
        /// <summary>
        /// 根据FId号执行软删除
        /// </summary>
        /// <param name="lid"></param>
        public bool softDelByFId(int fid)
        {
            string sql = "update FeedBack set IsDel=1 where FId=@FId;";
            SqlParameter[] paras = { 
                                      new SqlParameter("@FId",fid)
                                   };
            int r = dbhelper.ExecuteCommand(sql, paras);
            return r > 0;
        }
        #endregion

        #region 根据用户UserId反馈信息
        /// <summary>
        /// 反馈信息
        /// </summary>
        /// <returns></returns>
        public bool feedBack(int userid,string message)
        {
            string sql = "insert into FeedBack(UserId,[Message])values(@UserId,@Message);";
            SqlParameter[] paras = { 
                                      new SqlParameter("@UserId",userid),
                                      new SqlParameter("@Message",message)
                                   };
            int r = dbhelper.ExecuteCommand(sql, paras);
            return r > 0;
        } 
        #endregion

        #region 得到分页的反馈信息
        /// <summary>
        /// 得到分页的反馈信息
        /// </summary>
        /// <returns></returns>
        public List<MODEL.FeedBack> getAllFB(int pagenum)
        {
            string sql = "select * from("+
                          "  select row_number()over(order by FAddTime desc) row,*"+
                          "  from FeedBack fb join Users u on fb.UserId=u.UId"+
                          "  where IsDel=0"+
                          "  )as fb"+
                          "  {0};";
            sql = string.Format(sql, pagenum == 0 ? "" : "where fb.row>=4*(@pagenum-1)+1 and fb.row<=4*@pagenum");
            SqlParameter[] paras = { 
                                      new SqlParameter("@pagenum",pagenum)
                                   };
            DataTable dt = dbhelper.GetDataTable(sql,paras);
            if (dt.Rows.Count == 0)
            {
                return null;
            }
            else
            {
                List<MODEL.FeedBack> list = new List<MODEL.FeedBack>();
                foreach (DataRow dr in dt.Rows)
                {
                    MODEL.FeedBack model = new MODEL.FeedBack();
                    setDr2Model(dr, model);
                    if (model.Message.Length > 15)
                    {
                        model.Message = model.Message.Substring(0, 15)+"...";
                    }
                    list.Add(model);
                }
                return list;
            }
        } 
        #endregion

        #region 得到总页数
        /// <summary>
        /// 得到总页数
        /// </summary>
        /// <returns></returns>
        public int getPage()
        {
            string sql = "select count(*) from FeedBack where IsDel=0;";
            int count = dbhelper.GetScalar(sql);
            int pagecount = (int)Math.Ceiling(count / ((double)4));
            return pagecount;
        } 
        #endregion

        #region 把数据行转换成实体对象
        /// <summary>
        /// 把数据行转换成实体对象
        /// </summary>
        /// <param name="dr"></param>
        /// <param name="model"></param>
        public void setDr2Model(DataRow dr, MODEL.FeedBack model)
        {
            if (dr.Table.Columns.Contains("FId") && dr["FId"] != null)
            {
                model.FId = Convert.ToInt32(dr["FId"]);
            }
            if (dr.Table.Columns.Contains("UserId") && dr["UserId"] != null)
            {
                model.UserId = Convert.ToInt32(dr["UserId"]);
                MODEL.Users user = new MODEL.Users();
                new DAL.Users().setDr2Model(dr, user);
                model.User = user;
            }
            if (dr.Table.Columns.Contains("Message") && dr["Message"] != null)
            {
                model.Message = dr["Message"].ToString();
            }
            if (dr.Table.Columns.Contains("FAddTime") && dr["FAddTime"] != null)
            {
                model.FAddTime = Convert.ToDateTime(dr["FAddTime"]);
            }
            if (dr.Table.Columns.Contains("IsDel") && dr["IsDel"] != null)
            {
                model.IsDel = Convert.ToBoolean(dr["IsDel"]);
            }
        } 
        #endregion
    }
}
